<?php

namespace app\api\controller\driver;

use app\common\controller\Api;
use app\common\service\driver\OrderService;
use think\Validate;

/**
 * 司机端-抢单大厅接口
 * @ApiWeigh (89)
 */
class OrderTaking extends Api
{
    protected $beforeActionList = [
        'detail_before' => ['only' => 'detail,create'],
    ];
    protected $noNeedLogin = [''];
    protected $noNeedRight = ['*'];

    public function _initialize()
    {
        parent::_initialize();
    }

    /**
     * @ApiInternal
     */
    public function detail_before()
    {
        $params = $this->request->post();
        $rule = [
            'order_id|订单号' => 'require|gt:0',
        ];
        $message = [];
        $validate = new Validate($rule, $message);
        if (!$validate->check($params)) {
            $this->error($validate->getError());
        }
    }


    /**
     * 列表
     *
     * @ApiMethod (POST)
     * @ApiParams (name="page", type="integer", required=true, description="页数")
     * @ApiParams (name="page_size", type="integer", required=true, description="每页数量")
     * @ApiReturnParams (name=data,type="object",description=要返回的数据)
     * @ApiReturnParams (name=data.total,type="integer",description=总数量)
     * @ApiReturnParams (name=data.data,type="array",description=要返回的数据)
     * @ApiReturn ({"code":1,"msg":"订单列表","time":"1720497307","data":{"total":3,"data":[{"id":9,"channel_label":"渠道标签","user_id":"用户ID","driver_id":"司机ID","order_id":"订单号","out_order_id":"平台订单ID","avatar":"用户头像","user_name":"用户姓名","user_phone":"用户手机号","start_area_id":"起点区域ID","start_lat":"起点纬度","start_lng":"起点经度","start_poi_name":"起点Poi名称","start_address":"起点地址","end_area_id":"终点区域ID ","end_lat":"终点纬度","end_lng":"终点经度","end_poi_name":"终点Poi名称","end_address":"终点地址","appointment_time":"预约时间，时间戳 无预约时间表示即时订单","pay_type":"费用类型","rights_id":"权益ID","service_type":"服务类型:DRIVE=代驾,TRANSFER=取送车","createtime":"2024-07-09 11:13:15","updatetime":"2024-07-09 11:43:15","cancel_reason":"取消原因","status":"订单状态:1=已受理,2=已取消,11=已接单,13=验车完成,15=交车完成","desc":"订单备注","mileage":"里程（公里）","total_fee":"总金额","driver_amount":"佣金金额","accept_time":"接单时间","start_time":"验车时间","is_end":"是否已点击交车按钮:0=否,1=是","end_code":"交车码","end_time":"完成时间（交车时间 取消时间）","channel": {"name": "渠道名称","label": "渠道标签"}}]}})
     **/
    public function list()
    {
        $page = $this->request->post('page');
        $page_size = $this->request->post('page_size');
        $user = $this->auth->getUser();
        $list = OrderService::list_driver($this->request->controller() . '/' . __FUNCTION__,$user,$page, $page_size,0);
        $this->success('列表', $list);
    }

    /**
     * 详情
     *
     * @ApiMethod (POST)
     * @ApiParams (name="order_id", type="string", required=true, description="订单号")
     * @ApiReturnParams (name=data,type="object",description=要返回的数据)
     * @ApiReturn ({"code":1,"msg":"订单详情","time":"1720497307","data":{"id":9,"channel_label":"渠道标签","user_id":"用户ID","driver_id":"司机ID","order_id":"订单号","out_order_id":"平台订单ID","avatar":"用户头像","user_name":"用户姓名","user_phone":"用户手机号","start_area_id":"起点区域ID","start_lat":"起点纬度","start_lng":"起点经度","start_poi_name":"起点Poi名称","start_address":"起点地址","end_area_id":"终点区域ID ","end_lat":"终点纬度","end_lng":"终点经度","end_poi_name":"终点Poi名称","end_address":"终点地址","appointment_time":"预约时间，时间戳 无预约时间表示即时订单","pay_type":"费用类型","rights_id":"权益ID","service_type":"服务类型:DRIVE=代驾,TRANSFER=取送车","createtime":"2024-07-09 11:13:15","updatetime":"2024-07-09 11:43:15","cancel_reason":"取消原因","status":"订单状态:1=已受理,2=已取消,11=已接单,13=验车完成,15=交车完成","desc":"订单备注","mileage":"里程（公里）","total_fee":"总金额","driver_amount":"佣金金额","accept_time":"接单时间","start_time":"验车时间","is_end":"是否已点击交车按钮:0=否,1=是","end_code":"交车码","end_time":"完成时间（交车时间 取消时间）"}})
     **/
    public function detail()
    {
        $order_id = $this->request->post('order_id');
        $user = $this->auth->getUser();
        $info = OrderService::detail_driver($this->request->controller() . '/' . __FUNCTION__,$order_id,$user);
        $this->success('详情', $info);
    }

    /**
     * 接单
     *
     * @ApiMethod (POST)
     * @ApiParams (name="order_id", type="string", required=true, description="订单号")
     * @ApiReturnParams (name=data,type="object",description=要返回的数据)
     * @ApiReturn ({"code": 1,"msg": "接单成功","time": "1694660842","data": null})
     **/
    public function create()
    {
        $order_id = $this->request->post('order_id');
        $user = $this->auth->getUser();
        OrderService::create_driver($order_id, $user);
        $this->success('接单成功');
    }

}
